/*
 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva.
 *
 * This file may be distributed under the terms of the Q Public License
 * as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

package net.sf.fmj.media.codec.video;

import java.awt.Dimension;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

import javax.media.Buffer;
import javax.media.Codec;
import javax.media.Format;
import javax.media.format.VideoFormat;

import net.sf.fmj.media.AbstractCodec;
import net.sf.fmj.media.util.BufferToImage;
import net.sf.fmj.media.util.ImageToBuffer;

/**
 * 
 * 
 *
 */
public class ImageScaler extends AbstractCodec implements Codec
{
	// TODO: the filter graph builder won't build this into the filter graph,
	// if we specify, say a specific dimension for the output format that
	// requires the input to be scaled.
	private final Dimension DIMENSION = null; //new Dimension(160, 100);
	// TODO: all formats supported by BufferToImage
	private final Format[] supportedInputFormats = new Format[] {
			new VideoFormat(VideoFormat.RGB, null, -1, Format.byteArray, -1.0f),
			new VideoFormat(VideoFormat.RGB, null, -1, Format.intArray, -1.0f),
		};
	private final Format[] supportedOutputFormats = new Format[] {
			new VideoFormat(VideoFormat.RGB, DIMENSION, -1, Format.intArray, -1.0f),
		};

	public Format[] getSupportedInputFormats()
	{
		return supportedInputFormats;
	}

	public Format[] getSupportedOutputFormats(Format input)
	{
		if (input == null)
			return supportedOutputFormats;
		VideoFormat inputCast = (VideoFormat) input;
		final Format[] result = new Format[] {
				new VideoFormat(VideoFormat.RGB, DIMENSION, -1, Format.intArray, -1.0f)};
	
		return result;
	}

	private BufferToImage bufferToImage;
	
	public Format setInputFormat(Format format)
	{
		final VideoFormat videoFormat = (VideoFormat) format;
		if (videoFormat.getSize() == null)
			return null;	// must set a size.
		
//		logger.fine("FORMAT: " + MediaCGUtils.formatToStr(format));
		// TODO: check VideoFormat and compatibility
		bufferToImage = new BufferToImage((VideoFormat) format);
		return super.setInputFormat(format);
	}
	
	public int process(Buffer input, Buffer output) 
	{
		final BufferedImage image = (BufferedImage) bufferToImage.createImage(input);
		
		final Dimension inputSize = ((VideoFormat) inputFormat).getSize();
		final Dimension outputSize = ((VideoFormat) outputFormat).getSize();
		final double scaleX = ((double) outputSize.width) / ((double) inputSize.width);
		final double scaleY = ((double) outputSize.height) / ((double) inputSize.height);
		
		final BufferedImage scaled = scale(image, scaleX, scaleY);	// TODO: is the size exact?  what about rounding errors?
		
		System.out.println("scaled: " + scaled.getWidth() + "x" + scaled.getHeight());
		final Buffer b = ImageToBuffer.createBuffer(scaled, ((VideoFormat) outputFormat).getFrameRate());
		output.setData(b.getData());
		output.setLength(b.getLength());
		output.setOffset(b.getOffset());
		output.setFormat(b.getFormat());
		// TODO: what about format?
	
		return BUFFER_PROCESSED_OK;

		
	}
	private BufferedImage scale(BufferedImage bi, double scaleX, double scaleY)
    {
        AffineTransform tx = new AffineTransform();
        tx.scale(scaleX, scaleY);
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BICUBIC);
        return op.filter(bi, null);
    }
}
